﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Newtonsoft.Json;
using ProductionManage.Models.DataModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;
using test.demo.Model.WM仓储管理;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using wangyaxin.Qi.Dto;

namespace wangyaxin.Qi
{
    [ApiExplorerSettings(GroupName = "王佳琪")]
    public class QiAppService : ApplicationService, IQiAppService
    {
        private readonly IRepository<Client, int> _clientRepository;
        private readonly IRepository<WM_Warehouse, int> _warehouseRepository;
        private readonly IRepository<WM_SN,int> _snRepository;
        private readonly IRepository<WM_Issue_Header,int> _issueHeaderRepository;


        public QiAppService(IRepository<Client, int> clientRepository, IRepository<WM_Warehouse, int> warehouseRepository, IRepository<WM_SN, int> snRepository, IRepository<WM_Issue_Header, int> issueHeaderRepository)
        {
            _clientRepository = clientRepository;
            _warehouseRepository = warehouseRepository;
            _snRepository = snRepository;
            _issueHeaderRepository = issueHeaderRepository;
        }

        /// <summary>
        /// 客户查询
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetClientShow(string clientCode="", string clientName="", int page = 1, int limit = 3)
        {
            var clients = await _clientRepository.GetListAsync();
           
            var result = clients.Where(c => c.ClientCode.Contains(clientCode))
                .Where(c => c.ClientName.Contains(clientName))

                .Skip((page - 1) * limit)
            .Take(limit)
                .ToList();
      
            return JsonConvert.SerializeObject(new ResultResturn<Client> { code = "0", data = result, msg = "成功", count = clients.Count });
        }

       

        /// <summary>
        /// 客户添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddClient(CreateClientDto input)
        {
            var client = input.MapTo<Client>();
            var result = await _clientRepository.InsertAsync(client) != null ? 1 : 0;
            return result;
        }

        /// <summary>
        /// 客户修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> UpdateClientById(UpdateClientDto input)
        {

            var client = input.MapTo<Client>();
            var result = await _clientRepository.UpdateAsync(client) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// 客户删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelClient(int id)
        {

            await _clientRepository.DeleteAsync(id);
            return 1;

        }
        /// <summary>
        /// 仓库查询
        /// </summary>
        /// <param name="warehouseCode"></param>
        /// <param name="warehouseName"></param>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<string> GetWareHouse(string warehouseCode = "", string warehouseName = "", int page = 1, int limit = 3)
        {
            var house = await _warehouseRepository.GetListAsync();

            var result = house.Where(c => c.WarehouseCode.Contains(warehouseCode))
                .Where(c => c.WarehouseName.Contains(warehouseName))
                .Skip((page - 1) * limit).Take(limit).ToList();
            return JsonConvert.SerializeObject(new ResultResturn<WM_Warehouse> { code = "0", data = result, msg = "成功", count = house.Count });
        }

        /// <summary>
        /// 仓库添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddWareHouse(CreateWareHouseDto input)
        {
            var house = input.MapTo<WM_Warehouse>();
            house.CreateBy = "张三";
            house.CreateTime = DateTime.Now;
            var result = await _warehouseRepository.InsertAsync(house) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// 仓库修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> UpdateWareHouse(UpdateWareHouseDto input)
        {
            var house= input.MapTo<WM_Warehouse>();
            house.UpdateBy = "张三";
            house.UpdateTime = DateTime.Now;
            var result = await _warehouseRepository.UpdateAsync(house) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// 仓库删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelWareHouse(int id)
        {
            await _warehouseRepository.DeleteAsync(id);
            return 1;
        }

        /// <summary>
        /// SN查询
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetSnShow(string snCode = "", string itemCode = "", string itemName = "", string batchCode = "", int page = 1, int limit = 3)
        {
            var Sn = await _snRepository.GetListAsync();

            var result = Sn.WhereIf(snCode != "",c => c.SnCode.Contains(snCode))
                .WhereIf(itemCode != "", c => c.ItemCode.Contains(itemCode))
                .WhereIf(itemName != "", c => c.ItemName.Contains(itemName))
                .WhereIf(batchCode != "",c => c.BatchCode.Contains(batchCode))
                .Skip((page - 1) * limit)
            .Take(limit)
                .ToList();
            return JsonConvert.SerializeObject(new ResultResturn<WM_SN> { code = "0", data = result, msg = "成功", count = Sn.Count });
        }
        /// <summary>
        /// Sn添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddSn(CreateSnDto input)
        {
            var Sn = input.MapTo<WM_SN>();
            Sn.CreateBy = "王五";
            Sn.CreateTime = DateTime.Now;
            var result = await _snRepository.InsertAsync(Sn) != null ? 1 : 0;
            return result;
        }

        /// <summary>
        /// Sn修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> UpdateSn(UpdateSnDto input)
        {
            var Sn= input.MapTo<WM_SN>();
            Sn.UpdateBy = "老六";
            Sn.UpdateTime = DateTime.Now;
            var result = await _snRepository.UpdateAsync(Sn) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// Sn删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelSn(int id)
        {
            await _snRepository.DeleteAsync(id);
            return 1;
        }
        /// <summary>
        /// 生产领料查询
        /// </summary>
        /// <returns></returns>
        public async Task<string> GetHeader(string issueCode = "", string issueName = "", string warehouseName = "", string issueDate = "", string status = "", int page = 1, int limit = 3)
        {
            var header = await _issueHeaderRepository.GetListAsync();

            var result = header.WhereIf(issueCode != "", c => c.IssueCode.Contains(issueCode))
                .WhereIf(issueName != "", c => c.IssueName.Contains(issueName))
                .WhereIf(warehouseName != "", c => c.WarehouseName.Contains(warehouseName))
                .WhereIf(issueDate != "", c => c.IssueDate.ToString("yyyy-MM-dd").Contains(issueDate))
                .WhereIf(status != "", c => c.Status.Contains(status))
                .Skip((page - 1) * limit)
            .Take(limit)
                .ToList();
            return JsonConvert.SerializeObject(new ResultResturn<WM_Issue_Header> { code = "0", data = result, msg = "成功", count = header.Count });
        }
        /// <summary>
        /// 生产领料添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddHeader(CreateHeaderDto input)
        {
            var header = input.MapTo<WM_Issue_Header>();
            header.CreateBy = "鬼脚七";
            header.CreateTime = DateTime.Now;
            var result = await _issueHeaderRepository.InsertAsync(header) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// 生产领料修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> UpdateHeader(UpdateHeaderDto input)
        {
            var header = input.MapTo<WM_Issue_Header>();
            header.UpdateBy = "老八";
            header.UpdateTime = DateTime.Now;
            var result = await _issueHeaderRepository.UpdateAsync(header) != null ? 1 : 0;
            return result;
        }
        /// <summary>
        /// 生产领料删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DelHeader(int id)
        {
            await _issueHeaderRepository.DeleteAsync(id);   
            return 1;
        }





    }
}
